package com.gxa.insurance.service.impl;

import com.gxa.insurance.exception.BusinessException;
import com.gxa.insurance.exception.ExceptionCode;
import com.gxa.insurance.mapper.CardMapper;
import com.gxa.insurance.pojo.Card;
import com.gxa.insurance.service.CardService;
import com.gxa.insurance.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CardServiceImpl
 * @Description TODO
 * @Author ds
 * @Date 2022/6/7 22:08
 * @Version 1.0
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class CardServiceImpl implements CardService {

    @Autowired
    CardMapper cardMapper;
    @Override
    public void add(Card card) throws Exception{
        card.setRegTime(new Date(System.currentTimeMillis()));
        cardMapper.insert(card);
    }

    @Override
    public Map<String,Object> selectByPage(Integer page, Integer size) throws Exception {
        Map<String,Object> map = new HashMap();
        Integer count = cardMapper.selectCount();
        List<Card> cards = cardMapper.selectByPage((page - 1) * size, size);
        if(cards.size()==0){
            throw new BusinessException(ExceptionCode.CUSTOMER_IS_EMPTY);
        }
        map.put("count",count);
        map.put("cards",cards);
        return map;
    }

    @Override
    public List<Card> selectAll() throws Exception {
        List<Card> cards = cardMapper.selectAll();
        if (cards.size() == 0){
            throw new BusinessException(ExceptionCode.EMPTY_LIST);
        }
        return cards;
    }

    @Override
    public List<Card> selectById(Integer id) throws Exception {
        List<Card> cards = cardMapper.selectById(id);
        if(cards.size()==0){
            throw new BusinessException(ExceptionCode.EMPTY_LIST);
        }
        return cards;
    }

    @Override
    public Integer update(Card card) throws Exception {
        Integer rows = cardMapper.update(card);
        if(rows == 0) {
            throw new BusinessException(ExceptionCode.UPDATE_FILED);
        }
        return rows;
    }

    @Override
    public Integer delete(Integer id) throws Exception {
        Integer rows = cardMapper.deleteById(id);
        if(rows == 0) {
            throw new BusinessException(ExceptionCode.DELETE_FILED);
        }
        return rows;
    }

    @Override
    public Map<String, Object> getLikeList(Map map) {

        List<Card> cards = null;
        Map<String,Object> listMap = new HashMap();
        Object page = map.get("page");
        Object size = map.get("size");
        int intPage = Integer.parseInt(String.valueOf(page));
        int intPageSize = Integer.parseInt(String.valueOf(size));
        intPage=(intPage-1)*intPageSize;
        map.put("page",intPage);
        map.put("size",intPageSize);

        if(StringUtil.isEmpty((String)map.get("name"))){
            cards = cardMapper.selectByPage(intPage, intPageSize);
        }
        cards = cardMapper.selectLikeName(map);

        Integer count = cardMapper.selectLikeCount(map);
        listMap.put("cards",cards);
        listMap.put("count",count);
        return listMap;
    }

    @Override
    public List<Card> selectByCarId(String carId) throws BusinessException {

        List<Card> cards = cardMapper.selectByCarId(carId);

        if(cards.size() == 0){

            throw new BusinessException(ExceptionCode.EMPTY_LIST);
        }

        return cards;
    }
}
